<?php
/**
 * Created by PhpStorm.
 * User: lizi
 * Date: 2021/09/03
 * Time: 17:53
 */

namespace App\Console\Commands;

use App\Jobs\SendChangeOrderMessage;
use App\Models\AppMiniPlate;
use App\Models\AppTypeModel;
use App\Models\OrderReturn;
use App\Models\Suggest;
use App\Models\SuggestLog;
use App\Models\Order;
use App\Models\MemberOther;
use App\Services\AlipayMessageService;
use App\Services\AlipayService;
use App\Services\CommonService;
use App\Services\LogisticsService;
use App\Services\OrderService;
use App\Services\RadarService;
use Illuminate\Console\Command;

class ConfirmOrderCommand extends BaseCommand
{
    /**
     * The name and signature of the console command.
     * @var string
     */
    protected $signature = 'ConfirmOrder';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '查物流确认收货';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * */
    protected function handleExec()
    {
        $order=Order::query()->where('status',20)->orderBy('id','asc')
            ->select('id','status','mobile')
            ->get();
        foreach ($order as $item){
            $OrderReturn=OrderReturn::query()->where('order_id',$item['id'])->select('logistics_no','id')->first();
            if($OrderReturn){
                $express_data= LogisticsService::logisticsMessage($OrderReturn['logistics_no'],$OrderReturn['logistics_no'],$item['mobile']);

                var_dump($express_data);
                if($express_data&&$express_data['data'][0]['opcode']=='80'||$express_data['data'][0]['opcode']=='658'){
                    $data = [
                        'order_id'=>$item['id'],
                        'sign_proof'=>'',
                        'info'=>json_encode($express_data),
                        'status'=>OrderReturn::ORDER_EXPRESS_STATUS_SIGNED,
                        'confirm_time'=>time(),
                    ];
                }else{
                  continue;
                }

            $res = OrderReturn::where(['id'=>$OrderReturn['id']])->update($data);
            if(!$res){
                $this->infoLog('此订单物流信息签收失败',$res);
            }else{
                if($express_data&&$express_data['data'][0]['opcode']=='80'||$express_data['data'][0]['opcode']=='658'){
                    Order::query()->where(['id'=>$item['id']])->update(['status'=>30]);
                    SendChangeOrderMessage::dispatch($item['id'],AlipayMessageService::KX_SHOPPING,'SIGNED');
                }
            }
         }
      }
   }

}
